﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;

namespace WebComputers.Class
{
    public class CartManager : System.Web.UI.Page
    {
        public CartManager()
        {
            Create();
        }

        public void Create()
        {
            if (Session["Cart"] == null)
            {
                List<Cart> cart = new List<Cart>();
                Session["Cart"] = cart;
            }
        }

        public void Add(Cart cart)
        {
            try
            {
                if (Session["Cart"] != null)
                {
                    List<Cart> curCart = Session["Cart"] as List<Cart>;
                    if (curCart.Where(q => q.iDanhMucHangHoa_ID == cart.iDanhMucHangHoa_ID).Count() > 0)
                    {
                        int i = curCart.FindIndex(q => q.iDanhMucHangHoa_ID == cart.iDanhMucHangHoa_ID);
                        curCart[i].iSoLuong += cart.iSoLuong;
                        curCart[i].dThanhTien = curCart[i].dGiaBan * curCart[i].iSoLuong;

                    }
                    else
                    {
                        cart.dThanhTien = cart.dGiaBan * cart.iSoLuong;
                        curCart.Add(cart);
                    }
                    Session["Cart"] = curCart;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Minus(Cart cart)
        {
            try
            {
                if (Session["Cart"] != null)
                {
                    List<Cart> curCart = Session["Cart"] as List<Cart>;
                    if (curCart.Where(q => q.iDanhMucHangHoa_ID == cart.iDanhMucHangHoa_ID).Count() > 0)
                    {
                        int i = curCart.FindIndex(q => q.iDanhMucHangHoa_ID == cart.iDanhMucHangHoa_ID);
                        if (cart.iSoLuong < curCart[i].iSoLuong)
                        {
                            curCart[i].iSoLuong -= cart.iSoLuong;
                            curCart[i].dThanhTien = curCart[i].dGiaBan * curCart[i].iSoLuong;
                        }
                        else
                        {
                            curCart.RemoveAt(i);
                        }
                    }
                    Session["Cart"] = curCart;
                }
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public void Remove(Cart cart)
        {
            try
            {
                if (Session["Cart"] != null)
                {
                    List<Cart> curCart = Session["Cart"] as List<Cart>;
                    if (curCart.Where(q => q.iDanhMucHangHoa_ID == cart.iDanhMucHangHoa_ID).Count() > 0)
                    {
                        int i = curCart.FindIndex(q => q.iDanhMucHangHoa_ID == cart.iDanhMucHangHoa_ID);
                        curCart.RemoveAt(i);
                    }
                    Session["Cart"] = curCart;
                }
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public void Delete()
        {
            try
            {
                Session["Cart"] = null;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public decimal TotalCost()
        {
            try
            {
                decimal rtVal = 0;
                if (Session["Cart"] != null)
                {
                    List<Cart> curCart = Session["Cart"] as List<Cart>;
                    rtVal = curCart.Sum(q => q.dThanhTien);

                }
                return rtVal;
            }
            catch (Exception)
            {                
                return 0;
            }
        }

        public int TotalQuantity()
        {
            try
            {
                int rtVal = 0;
                if (Session["Cart"] != null)
                {
                    List<Cart> curCart = Session["Cart"] as List<Cart>;
                    rtVal = curCart.Sum(q => q.iSoLuong);              
                }
                return rtVal;
            }
            catch (Exception)
            {                
                return 0;
            }
        }

    }

}